// pages/operation/renewalAlert/renewalAlertIndex/index.js
import {
  RenewalAlert
} from "../../../../api/api-work/renewalAlert.js"
const renewalAlert = new RenewalAlert()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: "1",
    id: null,
    remainingClass: 0,
    remainingDay: 0,
    param: "",
    pageNum: 1,
    pageSize: 10,
    total: 0,
    tableDatas: [],
    istriggered: false,
    loadStatus: "",
    /**
     * 详情popUp数据
     */
    queryDatas: {},
    studentId: "", //选中学员id
    exceed: "", //超上的课时,通过剩余课时取绝对值自己计算
    isShowDetailsPopUp: false,
    name: "",
    mobile: "",
    courseName: "",
    className: "",
    lessonType: "", //授课方式
    remainderLesson: "", //剩余数量
    expireDate: "", //到期日期
    teacherName: "",
    counselorName: "",
    managementName: "",
    renewalReminderTime: "", //上次提醒时间
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    this.setData({
      pageNum: 1,
      tableDatas: [],
    })
    await this.getSettingsList()
  },
  //获取续费预警列表
  async getAlertsList() {
    renewalAlert.getAlertsListAPI({
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      wxParam: this.data.param,
      type: this.data.type
    }).then(res => {
      let rows = this.data.tableDatas;
      res.rows.map(item => {
        item.exceed = item.remainderLesson >= 0 ? '' : Math.abs(item.remainderLesson)
      })
      rows.push(...res.rows);
      this.setData({
        tableDatas: rows,
        total: res.total
      })
      console.log('11111', this.data.tableDatas);
      console.log('22222', this.data.total);
      if (this.data.total == 0) {
        this.setData({
          // 无数据
          loadStatus: ""
        })
      } else if (this.data.tableDatas.length < this.data.total) {
        // 还有数据
        this.setData({
          // 加载更多
          loadStatus: "-"
        })
      } else {
        // 没有数据了
        this.setData({
          // 加载更多
          loadStatus: "nomore"
        })
      }
    })
  },
  searchStudent() {
    this.setData({
      pageNum: 1,
      tableDatas: []
    })
    this.getAlertsList();
  },
  // 上拉加载
  getMoreDatas() {
    console.log('55555', this.data.pageNum);
    if (this.data.loadStatus === 'nomore') {
      // 没有更多了
      return;
    }
    this.setData({
      pageNum: this.data.pageNum + 1,
      loadStatus: "loading" // 加载中
    })
    this.getAlertsList();
  },

  // 下拉刷新
  refreshMoreDatas() {
    this.setData({
      istriggered: false,
      pageNum: 1,
      tableDatas: []
    })
    this.getAlertsList();
  },
  //获取预警值
  async getSettingsList() {
    renewalAlert.getSettingsListAPI().then(res => {
      console.log(res);
      this.setData({
        type: res.type,
        id: res.id,
        remainingClass: res.remainingClass,
        remainingDay: res.remainingDay,
      })
      this.getAlertsList()
    })
  },

  //去往预警设置
  // toSettingAlert() {
  //   const param = JSON.stringify({
  //     type: this.data.type,
  //     id: this.data.id,
  //     remainingClass: this.data.remainingClass,
  //     remainingDay: this.data.remainingDay,
  //   })
  //   wx.navigateTo({
  //     url: `/pages/operation/renewalAlert/settingAlert/index?param=${param}`,
  //   })
  // },

  //打开详情
  showDetailsPopUp(e) {
    const query = e.currentTarget.dataset.query;
    this.setData({
      queryDatas: query,
      studentId: query.studentId,
      isShowDetailsPopUp: true,
      name: query.name,
      mobile: query.mobile,
      courseName: query.courseName,
      className: query.className,
      lessonType: query.lessonType, //授课方式
      remainderLesson: query.remainderLesson, //剩余数量
      expireDate: query.expireDate && query.expireDate.slice(0, 10), //到期日期
      teacherName: query.teacherName,
      counselorName: query.counselorName,
      managementName: query.managementName,
      renewalReminderTime: query.renewalReminderTime, //上次提醒时间
      exceed: query.remainderLesson >= 0 ? '' : Math.abs(query.remainderLesson)
    })
  },
  //关闭详情
  onCloseDetailsPopUp() {
    this.setData({
      isShowDetailsPopUp: false
    })
  },
  //去学员详情
  toDetails() {
    const obj = {
      id: this.data.studentId
    }
    const data = JSON.stringify(obj);
    wx.navigateTo({
      url: `/pages/operation/clueManage/clueDetail/index?obj=${data}`,
    })
  },
  //去往报名续费
  toSignUp() {
    this.setData({
      isShowDetailsPopUp: false
    })
    const param = JSON.stringify({
      id: this.data.studentId
    })
    wx.navigateTo({
      url: `/pages/operation/signUp/signUpMain/index?param=${param}&&operateType=1`,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})